/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package at.prospect.manualcreator.controller;



import at.prospect.manualcreator.entity.Manual;
import at.prospect.manualcreator.session.ManualFacade;
import com.lowagie.text.BadElementException;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import javax.servlet.ServletContext;


@ManagedBean(name="PrintManualsBean")
@RequestScoped
public class PrintManualsBean {
  
    
    /*
     * Das ist also der Trick
     */
    @EJB
    ManualFacade mf;
    
    
	public PrintManualsBean() {
            
	}


	public List<Manual> getManuals() {
	    
            return mf.findAll();
                        
	}
        
        
        public void preProcessPDF(Object document) throws IOException, BadElementException, DocumentException {
            
            Document pdf = (Document) document;
                        
            pdf.open();
            pdf.setPageSize(PageSize.A4);
            Image image;

            ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
            String logo = servletContext.getRealPath("") + File.separator + "resources" + File.separator + "image" + File.separator + "logo.gif";

            image = Image.getInstance(logo);
            image.setAlignment(Chunk.ALIGN_CENTER);
            
            pdf.add(Chunk.NEWLINE);
            pdf.add(Chunk.NEWLINE);
            pdf.add(Chunk.NEWLINE);
            pdf.add(Chunk.NEWLINE);
            pdf.add(Chunk.NEWLINE);
            pdf.add(Chunk.NEWLINE);
            pdf.add(Chunk.NEWLINE);
            pdf.add(Chunk.NEWLINE);
            pdf.add(Chunk.NEWLINE);
            pdf.add(Chunk.NEWLINE);
            
            pdf.add(image);
            
            pdf.add(Chunk.NEWLINE);
            
            pdf.addAuthor("Prospect Unternehmensberatung");
            pdf.addCreationDate();
            pdf.addHeader("Header", "Manual");
            
            pdf.addTitle("Prospect Unternehmensberatung Manual");
            
            pdf.newPage();
            

            
            
}        
        
}
				
